package com.itwn.controller;


import com.github.pagehelper.PageInfo;
import com.itwn.entity.Manager;
import com.itwn.entity.ManagerRole;
import com.itwn.entity.Role;
import com.itwn.result.ResponseResult;
import com.itwn.service.ManagerService;
import com.itwn.service.RoleService;
import com.itwn.vo.ManagerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin
@Transactional
@RequestMapping("managerment")
@Api(value = "管理员操作")
public class ManagerController {
    @Autowired
    ManagerService managerService;

    @ApiOperation(value = "管理员列表显示")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",name = "userName",value = "管理员用户名",
                    dataType = "String",required = false),
    })
    @GetMapping("/showList")
    public ResponseResult<PageInfo<ManagerVo>> showList(String text,String userCode,String userName,
                                                      @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                                      @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize){
        ResponseResult<PageInfo<ManagerVo>> reuslt = new ResponseResult<>();
        boolean b = text.matches("\\d+");
        if(b){
            userCode=text;
        }else {
            userName=text;
        }
        PageInfo<ManagerVo> pageInfo = managerService.queryAllManager(userName,userCode, pageNum, pageSize);
        System.out.println(pageInfo.getList());
        reuslt.setCode(200);
        reuslt.setMsg("查询成功！");
        reuslt.setData(pageInfo);
        return reuslt;
    }


    @Autowired
    RoleService roleService;

    @GetMapping("/showRole")
    public ResponseResult<List<Role>> showRole(){
        ResponseResult<List<Role>> result = new ResponseResult<>();
        List<Role> roles = roleService.queryAllRole();
        result.setCode(200);
        result.setMsg("查询成功！");
        result.setData(roles);
        return result;
    }

//    添加管理员
    @ApiOperation(value = "添加管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",name = "userCode",value = "管理员工号",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "userName",value = "管理员用户名",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "userPassword",value = "管理员密码",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "cellphone",value = "管理员手机",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "email",value = "管理员邮箱",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "locked",value = "管理员状态",
                    dataType = "String",required = false),
    })
    @PostMapping("/addManager")
    public ResponseResult<ManagerVo> addManager(@RequestBody ManagerVo managerVo){
        ResponseResult<ManagerVo> result = new ResponseResult<>();
        managerVo.setInsertTime(new Date());
        managerVo.setUpdateTime(new Date());
        int i=0;
        try{
            managerService.addManager(managerVo);
            String userCode = managerVo.getUserCode();
//            根据前台传回来的userCode查询出刚刚保存的管理员数据
            Manager manager = managerService.findByPrimaryKey(userCode);
            Long managerId = manager.getId();
            String roleIds = managerVo.getRoleIds();
            String[] ids = roleIds.split(",");
            ArrayList<Integer> list = new ArrayList<>();
            for (String id : ids) {
                list.add(Integer.valueOf(id));
            }
            for (Integer roleId : list) {
                i = managerService.addRole(managerId, new Long(roleId), new Date(), new Date());
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
        if(i==202){
            result.setCode(200);
            result.setMsg("添加成功！");
            return result;
        }else {
            result.setMsg("添加失败！");
            return result;
        }
    }

//    修改管理员

    @ApiOperation(value = "修改管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",name = "userCode",value = "管理员工号",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "userName",value = "管理员用户名",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "userPassword",value = "管理员密码",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "cellphone",value = "管理员手机",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "email",value = "管理员邮箱",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "locked",value = "管理员状态",
                    dataType = "String",required = false),
    })
    @PostMapping("/modifyManager")
    public ResponseResult<ManagerVo> modifyManager(@RequestBody ManagerVo managerVo){
        ResponseResult<ManagerVo> result = new ResponseResult<>();
        try{
            String userCode = managerVo.getUserCode();
            Manager manager = managerService.findByPrimaryKey(userCode);
            managerVo.setUpdateTime(new Date());
            managerService.modifyManager(managerVo);
            Long id = managerVo.getId();
            int i = managerService.removeManagerRoleById(id);
            String roleIds = managerVo.getRoleIds();
            Date insertTime = manager.getInsertTime();
            String[] ids = roleIds.split(",");
            ArrayList<Integer> list = new ArrayList<>();
            for (String id1 : ids) {
                list.add(Integer.valueOf(id1));
            }
            for (Integer roleId : list) {
                i = managerService.addRole(id, new Long(roleId), insertTime, new Date());
            }
        if(i>0){
            result.setCode(200);
            result.setMsg("修改成功！");
            return result;
        }else {
            result.setCode(101);
            result.setMsg("修改失败！");
            return result;
        }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @ApiOperation(value = "删除管理员")
    @ApiImplicitParams({
            @ApiImplicitParam(
                    paramType = "query",name = "id",value = "管理员id",
                    dataType = "String",required = false),
            @ApiImplicitParam(
                    paramType = "query",name = "roleId",value = "角色id",
                    dataType = "String",required = false),
    })
    @DeleteMapping("/removeManager/{id}/{roleCode}")
    public ResponseResult<Manager> removeManager(@PathVariable("id") Long id,@PathVariable("roleCode") Long roleCode){
        ResponseResult<Manager> result = new ResponseResult<>();
        try {
            managerService.removeManagerRole(id,roleCode);
            List<ManagerRole> managerRoles= managerService.queryManagerRole(id);
            if(managerRoles.size()==0) {
                managerService.removeById(id);
                result.setCode(200);
                result.setMsg("删除成功");
                return result;
            }else {
                result.setCode(200);
                result.setMsg("删除成功");
                return result;
            }
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }
}
